<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class GenerateModule extends Command
{
    protected $signature = 'make:module {name}';
    protected $description = '生成自定义模块，包括 Controller, Model, Service, Collection, Resource 等';

    public function handle()
    {
        $name = $this->argument('name');
        if (empty($name)) {
            $this->info('缺少name参数');
            return false;
        }

        //
        // 获取字符串的第一个字符
        $firstChar = $name[0];
        // 检查第一个字符是否为大写字母
        if (ctype_upper($firstChar)) {
            // 将第一个字符转换为小写，并与剩余的字符串拼接
            $lowerName =  strtolower($firstChar) . substr($name, 1);
        }else{
            $lowerName = $name;
        }
        $snakeName = $this->camelToSnake($name);

        $this->generateController($name, $lowerName);
        $this->generateModel($name, $lowerName,$snakeName);
        $this->generateService($name, $lowerName);
        $this->generateCollection($name, $lowerName);
        $this->generateResource($name, $lowerName);

        $this->info("模块 $name 生成成功！");
    }

    protected function generateController($name, $lowerName)
    {
        $path = app_path("Http/Controllers/{$name}Controller.php");
        if (!File::exists($path)) {
            $controllerTemplate = File::get(base_path('stubs/controller.stub'));
            $controllerTemplate = str_replace('{{name}}', $name, $controllerTemplate);
            $controllerTemplate = str_replace('{{lowerName}}', $lowerName, $controllerTemplate);
            File::put($path, $controllerTemplate);
            $this->info("Controller {$name}Controller 生成成功！");
        } else {
            $this->info("Controller {$name}Controller 已存在，跳过生成！");
        }
    }

    protected function generateModel($name, $lowerName,$snakeName)
    {
        $path = app_path("Models/{$name}.php");
        if (!File::exists($path)) {
            $modelTemplate = File::get(base_path('stubs/model.stub'));
            $modelTemplate = str_replace('{{name}}', $name, $modelTemplate);
            $modelTemplate = str_replace('{{lowerName}}', $lowerName, $modelTemplate);
            $modelTemplate = str_replace('{{snakeName}}', $snakeName, $modelTemplate);
            File::put($path, $modelTemplate);
            $this->info("Model {$name} 生成成功！");
        } else {
            $this->info("Model {$name} 已存在，跳过生成！");
        }
    }

    protected function generateService($name, $lowerName)
    {
        $path = app_path("Http/Services/{$name}Service.php");
        if (!File::exists($path)) {
            $serviceTemplate = File::get(base_path('stubs/service.stub'));
            $serviceTemplate = str_replace('{{name}}', $name, $serviceTemplate);
            $serviceTemplate = str_replace('{{lowerName}}', $lowerName, $serviceTemplate);
            File::put($path, $serviceTemplate);
            $this->info("Service {$name}Service 生成成功！");
        } else {
            $this->info("Service {$name}Service 已存在，跳过生成！");
        }
    }

    protected function generateCollection($name, $lowerName)
    {
        $path = app_path("Http/Resources/{$name}Collection.php");
        if (!File::exists($path)) {
            $collectionTemplate = File::get(base_path('stubs/collection.stub'));
            $collectionTemplate = str_replace('{{name}}', $name, $collectionTemplate);
            $collectionTemplate = str_replace('{{lowerName}}', $lowerName, $collectionTemplate);
            File::put($path, $collectionTemplate);
            $this->info("Collection {$name}Collection 生成成功！");
        } else {
            $this->info("Collection {$name}Collection 已存在，跳过生成！");
        }
    }

    protected function generateResource($name, $lowerName)
    {
        $path = app_path("Http/Resources/{$name}Resource.php");
        if (!File::exists($path)) {
            $resourceTemplate = File::get(base_path('stubs/resource.stub'));
            $resourceTemplate = str_replace('{{name}}', $name, $resourceTemplate);
            $resourceTemplate = str_replace('{{lowerName}}', $lowerName, $resourceTemplate);
            File::put($path, $resourceTemplate);
            $this->info("Resource {$name}Resource 生成成功！");
        } else {
            $this->info("Resource {$name}Resource 已存在，跳过生成！");
        }
    }

    /**
     * 字符串驼峰形式转为下划线形式
     * @param $str
     * @return string
     */
    public function camelToSnake($str) {
        $snake = '';
        $len = strlen($str);

        for ($i = 0; $i < $len; $i++) {
            $char = $str[$i];

            // 如果当前字符是大写且不是字符串的第一个字符，则在其前添加下划线
            if (ctype_upper($char) && $i !== 0) {
                $snake .= '_';
            }

            // 将当前字符转换为小写并添加到结果字符串中
            $snake .= strtolower($char);
        }

        return $snake;
    }
}
